import { service as request, globals } from '@/utils/request'
export default {
  data() {
    return {
      offsetLeft: 10,
      headerBarFixed: false
    }
  },
  computed: {
    left() {
      return this.offsetLeft + 'px'
    },
    goodsnum() {
      return this.$store.getters.goodsnum
    }
  },
  destroyed() {
    window.removeEventListener('scroll', this.onScroll())
  },
  created() {
    this.leftHandler()
    this.addScrollEventListen()
    this.addWindowResizeListen()
  },
  methods: {
    getScrollTop() {
      let scrollTop = 0
      if (document.documentElement && document.documentElement.scrollTop) {
        scrollTop = document.documentElement.scrollTop
      } else if (document.body) {
        scrollTop = document.body.scrollTop
      }
      return scrollTop
    },
    getClientHeight() {
      let clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight)
      } else {
        clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight)
      }
      return clientHeight
    },
    getScrollHeight() {
      return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)
    },
    onScroll(e) {
      const scrollTop = this.getScrollTop()
      if (scrollTop >= 35) {
        this.headerBarFixed = true
      } else {
        this.headerBarFixed = false
      }
      if (scrollTop >= 200) {
        this.affixShow = true
      } else {
        this.affixShow = false
      }
      const scrollHeight = this.getScrollHeight()
      const clientHeight = this.getClientHeight()
      const scrollDiff = scrollHeight - clientHeight - scrollTop
    },
    addScrollEventListen() {
      window.addEventListener('scroll', (e) => {
        this.onScroll(e)
      })
    },
    leftHandler() {
      const windowWidth = window.innerWidth
      const left = (windowWidth - 1180) / 2
      this.offsetLeft = left <= 72 ? 72 : left
    },
    addWindowResizeListen() {
      window.onresize = () => {
        this.leftHandler()
      }
    },
    getGoodsnum() {
      this.$store.dispatch('user/getGoodsnum')
    },
    toShoppingCart() {
      // this.$router.push({ path: '/shoppingcart' })
      const url = globals.mallPlateformUrl + '#/shoppingcart'
      window.open(url, '_blank')
      /* const routeData = this.$router.resolve({
        path: '/shoppingcart'
      })
      window.open(routeData.href, '_blank')*/
    },
    getService() { // goto/customer/service
      request({
        url: globals.userCenter + 'goto/customer/service?type=sub',
        method: 'get'
      }).then(res => {
        const newStr = res.replace('redirect:', '')
        window.open(newStr, '_blank')
      })
    }
  }
}
